// pages/my/order-detail/order-detail.js
const controllers = require('../../controllers/ControllerMy').controller;
const Controller = require("../../controllers/Controller").controller

const app = getApp()
function InitTime(endtime){
  var dd,hh,mm,ss = null;
  var time = (new Date(endtime.replace(/-/g,'/')).getTime() - new Date().getTime())/1000;
  if(time<=0){
      return '结束'
  }else{
      dd = Math.floor(time / 60 / 60 / 24);
      hh = Math.floor((time / 60 / 60) % 24);
      mm = Math.floor((time / 60) % 60);
      ss = Math.floor(time  % 60);
      var str = dd+"天"+hh+":"+mm+":"+ss;
      return str;
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    detail: {},
    imgurl: '',
    orderNo: 0,
    goodsId: 0,
    payType:'',
    isshow:false,
    singleord:'',
    buttons: [{text: '取消'}, {text: '确定'}],
    downtime:'',
    timer:'',
    adesc:'',
    odesc:'',
    shopId:""
  },
//按钮事件
tapDialogButton(e) {
  console.log(e);
  if (e.detail.index == 0) {
    this.setData({
      isshow: false
    })
  } else {
    this.cancelOrder()
    this.setData({
      isshow:false
    })
  }
},
 //显示取消确定弹窗
 toshow(e){
  this.setData({
    isshow:true,
    singleord:e.currentTarget.id
  })
},
  //取消订单
  cancelOrder() {
    controllers.cancelOrder({
      shopId: app.globalData.shopId,
      orderNo: this.data.singleord,
      type:0
    }).then(res => {
      wx.showToast({
        title: '取消成功',
        icon:'none'
      })
      setTimeout(function(){
        // wx.navigateTo({
        //   url: '/pages/my/product-order/product-order',
        // })
        wx.navigateBack({
          complete: (res) => {},
        })
      },1000)
    })
  },

  //跳转到商品详情页
  toShopDetail(e) {
    wx.navigateTo({
      url: '/pages/Index/Service?goodsid=' + this.data.goodsId+'&orderId='+this.data.detail.id+'&orderNo='+this.data.orderNo,
    })
  },

  //跳转到评论页面
  toComment(e) {
    wx.navigateTo({
      url: '/pages/my/service-comment/service-comment?id=' + e.currentTarget.id + '&type=' + e.currentTarget.dataset.type+'&from='+e.currentTarget.dataset.from,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.shopId){
      this.setData({
        shopId:options.shopId
      })
      app.globalData.shopId=shopId;
    }
    if(wx.getStorageSync('shopId')){
      this.setData({
        shopId:wx.getStorageSync('shopId')
      })
      app.globalData.shopId=wx.getStorageSync('shopId');
    }
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      imgurl:app.globalData.IMG_URL
    })
    this.setData({
      orderNo: options.id
    })
    let shopId=options.shopId || app.globalData.shopId
    if(!wx.getStorageSync('usertoken')){
      this.GetLogin()
    }else{
    //获取订单详情
     this.getOrderDetail()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  backindex(e){
    let page=getCurrentPages()
    let shopId=this.data.shopId
    console.log(page.length)
    if(page.length<=1){
      wx.redirectTo({
        url: '/pages/Index/Index?shopId='+shopId,
      })
    }
  },

  //没有token时执行登录函数，后执行请求
   GetLogin(flag) {
     let that=this;
    if (flag == undefined || flag) {
      wx.showLoading({
        title: '登录中...',
        mask: true
      });
    }
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        Controller.appletLogin({
          code: res.code
        }).then(result => {
          console.log(result.data)
          app.globalData.sessionKey=result.data.sessionKey
          wx.setStorageSync('usertoken', result.data.sessionId)
          that.getOrderDetail()
          app.globalData.openId=result.data.openId
          if (flag == undefined || flag) {
            wx.hideLoading();
          }
        }).catch(res=>{
          wx.hideLoading();
        })
        
      }
    });
  },

  //获取订单详情
  getOrderDetail(){
    let shopId=this.data.shopId
    let orderNo=this.data.orderNo
    let that=this;
    console.log(shopId,orderNo);
    controllers.getOrderDetail({
      shopId: shopId,
      orderNo: orderNo,
      type: 0
    }).then(res => {
      that.setData({
        detail: res.data,
        goodsId:res.data.goodsList[0].goodsId,
        odesc:res.data.goodsList[0].desc || '',
        adesc:res.data.goodsList[0].agreeDesc|| ''
      })
      if (that.data.detail.marketAccountList && that.data.detail.marketAccountList[0].endTime) {
        that.setData({
          timer: setInterval(() => {
            that.data.downtime = InitTime(that.data.detail.marketAccountList[0].endTime.replace(/-/g, '/'))
            that.setData({
              downtime: that.data.downtime
            })
          }, 1000)
        })
      }
      if(res.data.payType==0){
        that.setData({
          payType:'支付宝'
        })
      }else if(res.data.payType==1){
        that.setData({
          payType:'微信'
        })
      }else if(res.data.payType==2){
        that.setData({
          payType:'余额'
        })
      }else if(res.data.payType==3){
        that.setData({
          payType:'积分'
        })
      }else{
        that.setData({
          payType:'现金'
        })
      }
    })
  }
})